"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : dolphin插件
Case Name   : 兼容b库下删除public模式，合理报错
Create At   : 2023.01.10
Owner       : @zou_jialiang050
Description :
    1.创建兼容B库
    2.B库下删除public模式
    3.清理环境
Expect      :
    1.成功
    2.合理报错
    3.成功
History     : 
"""

import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node


class DESC(unittest.TestCase):
    def setUp(self):
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.pri_node = Node('PrimaryDbUser')
        self.constant = Constant()
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.database = 'd_compatibility_0017'

    def test_desc(self):
        text1 = 'step1:创建兼容B库 expect:成功'
        self.log.info(text1)
        create_database = self.pri_sh.execut_db_sql(
            f'''drop database if exists {self.database};
            create database {self.database} dbcompatibility='B';''')
        self.log.info(create_database)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, create_database,
                      '执行失败' + text1)

        text3 = 'step2:删除B库public模式 expect:报错'
        self.log.info(text3)
        sql_cmd = self.pri_sh.execut_db_sql('drop schema public;',
                                            dbname=self.database)
        self.log.info(sql_cmd)
        self.assertIn('ERROR:  cannot drop schema public because dolphin '
                      'depends on it', sql_cmd, '执行失败' + text3)

    def tearDown(self):
        text5 = 'step3:清理环境 expect:成功'
        self.log.info(text5)
        clean_environment1 = self.pri_sh.execut_db_sql(
            f'''drop database {self.database};''')
        self.log.info(clean_environment1)
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, clean_environment1,
                      '执行失败' + text5)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
